<template>
	<sw-common-frame :loading="loading">
		<template slot="button">
			<el-button size="small" type="primary" icon="el-icon-plus" @click="handleOpenDialog()">新增</el-button>
		</template>
		<template slot="query">
			<el-form size="small" inline>
				<el-form-item>
					<el-input v-model="queryData.name" clearable placeholder="部门名称" style="width: 200px"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" icon="el-icon-search" @click="handleQueryData()">查询</el-button>
				</el-form-item>
			</el-form>
		</template>
		<template slot="table">
			<el-table size="medium" :data="tableListData" height="100%" v-bind="tableConfig" @row-click="handleRowClick">
				<el-table-column prop="name" label="部门名称" min-width="300" show-overflow-tooltip></el-table-column>
				<el-table-column prop="managerName" label="负责人" min-width="120"></el-table-column>
				<el-table-column prop="createTime" label="创建时间" min-width="160" />
				<el-table-column label="操作" width="210" align="right" header-align="left">
					<el-button-group slot-scope="{ row }">
						<el-button size="mini" type="info" @click.stop="handleAddChildren(row)">添加子级</el-button>
						<el-button size="mini" type="warning" @click.stop="handleOpenDialog(row.id)">修改</el-button>
						<el-button size="mini" type="danger" @click.stop="handleDeleteData(row)">删除</el-button>
					</el-button-group>
				</el-table-column>
			</el-table>
		</template>
		<form-dialog v-model="formDialog" :data-id="dataId" :list="tableData" :is-sub.sync="isChildren" @refresh="queryTableData()"></form-dialog>
	</sw-common-frame>
</template>

<script>
import lodash from 'lodash';
import formDialog from './form';
import listMixins from '@/mixins/listMixins';
import { getDepartTree, deleteDepart } from '@/api/system/tbSysDept';
export default {
	mixins: [listMixins],
	components: { formDialog },
	data() {
		return {
			pageName: '部门',
			isChildren: false,
			tableConfig: {
				ref: 'table',
				rowKey: 'id',
				size: 'medium',
				defaultExpandAll: true,
				treeProps: { children: 'childList', hasChildren: 'hasChildren' }
			},
			queryName: ''
		};
	},
	computed: {
		listFunction() {
			return eval(getDepartTree);
		},
		deleteFunction() {
			return eval(deleteDepart);
		},
		requestParams() {
			return { pId: '0' };
		},
		tableListData() {
			let name = this.queryName.toLowerCase();
			let filterTreeData = (nodes) => {
				let filtered = [];
				nodes.forEach((node) => {
					if (node.name.toLowerCase().includes(name)) {
						let child = node.childList ? filterTreeData(node.childList) : [];
						if (child.length) {
							filtered.push({ ...node, childList: child });
						} else {
							filtered.push(node);
						}
					} else if (node.childList) {
						let childList = filterTreeData(node.childList);
						if (childList.length) {
							filtered.push({ ...node, childList });
						}
					}
				});
				return filtered;
			};
			return filterTreeData(this.tableData);
		}
	},
	watch: {
		queryName() {
			this.$refs['table'].doLayout();
		}
	},

	methods: {
		//查询部门
		handleQueryData() {
			this.queryName = lodash.cloneDeep(this.queryData.name);
		},

		//添加子级
		handleAddChildren(data) {
			this.dataId = data.id;
			this.formDialog = true;
			this.isChildren = true;
		},

		//行点击事件
		handleRowClick(row, column, event) {
			this.$refs['table'].toggleRowExpansion(row);
		}
	}
};
</script>

<style lang="scss" scoped></style>
